package com.ruoyi.business.aop;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ruoyi.business.domain.BusDevicePressure;
import com.ruoyi.business.domain.BusNeighborhoodLedger;
import com.ruoyi.business.service.IBusDevicePressureService;
import com.ruoyi.business.service.IBusNeighborhoodLedgerService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class BusDevicePressureAop {
    @Autowired
    private IBusDevicePressureService busDevicePressureService;

    @Autowired
    private IBusNeighborhoodLedgerService busNeighborhoodLedgerService;
    /**
     * 更新设备压力数量
     *监听 调压设施台账的Insert方法
     * @param p p
     * @return {@link Object }
     * @throws Throwable
     */
    @Around("execution(* com.ruoyi.business.service.IBusDevicePressureService.insertBusDevicePressure(..))")
    public Object updateDevicePressureCount(ProceedingJoinPoint p) throws Throwable {


        // 下面这个方法是分界点  上面是方法执行前，下面是方法执行后
        Object result = p.proceed();

        Object[] args = p.getArgs();

        System.out.println("args:" + Arrays.toString(args));
        Long neighborhoodId = null;
//      获取对象的小区id，
        if (args != null && args.length > 0) {
//           转换成对象
            BusDevicePressure pressure = (BusDevicePressure) args[0];

            neighborhoodId = pressure.getNeighborhoodId();
            System.out.println("neighborhoodId: " + neighborhoodId);
        } else {
            System.err.println("无法解析对象数组");
        }

//        判断小区id是否为null
        if (neighborhoodId != null){
            //        在数据库的设施表中查询小区的id对应的设施数量
            Long DeviceCount = busDevicePressureService.getCountByNeighborhoodId(neighborhoodId);
            if (DeviceCount != null){
                BusNeighborhoodLedger busNeighborhoodLedger = new BusNeighborhoodLedger(neighborhoodId,null,DeviceCount);
//        更新小区楼栋数
                busNeighborhoodLedgerService.updateBusNeighborhoodLedger(busNeighborhoodLedger);
                System.out.println("更新成功");
            }
        }
        return result;
    }

    /**
     *  监听 调压设施台账的Delete方法
     *更新小区的调压设施数量
     * @param p 切入点
     * @return {@link Object }
     * @throws Throwable
     */
    @Around("execution(* com.ruoyi.business.controller.BusDevicePressureController.remove(..))")
    public Object updateDevicePressureCountDelete(ProceedingJoinPoint p) throws Throwable {
//        在方法执行前获取要删除数据的小区id
        Object[] args = p.getArgs();
        //      获取要删除数据的小区id
        Long[] pressureId = null;
        Long[] neighborhoodId = null;
            // 检查是否为 Long 数组
            if (args != null && args.length > 0 && args[1] instanceof Long[]) {
                pressureId = (Long[]) args[0];
//                for (Long value : pressureId) {
//                    System.out.println(value);
//                }
//               数量发生改变的小区id
                neighborhoodId = (Long[]) args[1];

                for (Long value : neighborhoodId) {
                    System.out.println("neighborhoodId:");
                    System.out.println(value);
                }
            } else {
                System.err.println("无法解析 Long 数组");
            }
        Object result = null;
//            创建一个小区的对象集合
        List<BusNeighborhoodLedger> busNeighborhoodLedgerList = new ArrayList<>();
        // 下面这个方法是分界点  上面是方法执行前，下面是方法执行后
        try {
            result = p.proceed();

        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            //      在删除后，查询改小区的设施数量
            if (pressureId != null){
//            返回一个list
                System.out.println("neighborhoodId:" + Arrays.toString(neighborhoodId));
                List<Long> regulatorNum = busDevicePressureService.getCountByNeighborhoodIds(neighborhoodId);
//            遍历对象   根据传入的值的顺序返回结果
                for (Long value : regulatorNum) {
                    System.out.println("DeviceCount:");

                    System.out.println(value);
                }
//         往集合中加入数据
                for (int i = 0; i < regulatorNum.size(); i++){
                    Long id = neighborhoodId[i];
                    Long dc = regulatorNum.get(i);
                    System.out.println("id:" + id + " regulatorNum:" + regulatorNum.get(i));

                    busNeighborhoodLedgerService.updateBusNeighborhoodLedger( new BusNeighborhoodLedger(id,null,dc));
//                busNeighborhoodLedgerList.add(new BusNeighborhoodLedger(id,null,dc));
                }
//        修改小区设施数量    批量更新数据
//            busNeighborhoodLedgerService.updateBusNeighborhoodLedgers(busNeighborhoodLedgerList);
//            busNeighborhoodLedgerList.forEach(System.out::println);
//            if (DeviceCount != null){
//                BusNeighborhoodLedger busNeighborhoodLedger = new BusNeighborhoodLedger(neighborhoodId,null,DeviceCount);
////        更新小区楼栋数
//                busNeighborhoodLedgerService.updateBusNeighborhoodLedger(busNeighborhoodLedger);
                System.out.println("更新成功");
//            }
            }

        }
        return result;
    }

//    检测单个删除操作
    @Around("execution(* com.ruoyi.business.controller.BusDevicePressureController.singleremove(..))")
    public Object updateDevicePressureCountSingleDelete(ProceedingJoinPoint p) throws Throwable {

        Object[] args = p.getArgs();

        System.out.println("args:" + Arrays.toString(args));
        //      获取要删除数据的小区id
        Long[] pressureId = null;

        if (args != null && args.length > 0 && args[0] instanceof Long[]) {
            pressureId = (Long[]) args[0];
//            for (Long value : pressureId) {
//                System.out.println(value);
//            }
        } else {
            System.err.println("无法解析 Long 数组");
        }

        Object result = null;
//            在删除前们获取对象的小区id

        BusDevicePressure pressure = busDevicePressureService.selectBusDevicePressureById(pressureId[0]);
        Long neighborhoodId = pressure.getNeighborhoodId();
        try {
            result = p.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            //      在删除后，查询改小区的设施数量
            if (pressureId != null){

//               根据对象的id 获取 对应小区的总数
                //        在数据库的设施表中查询小区的id对应的设施数量
                Long DeviceCount = busDevicePressureService.getCountByNeighborhoodId(neighborhoodId);

                busNeighborhoodLedgerService.updateBusNeighborhoodLedger(new BusNeighborhoodLedger(neighborhoodId,null,DeviceCount));
           }
                System.out.println("更新成功");


        }

        return result;
    }


}
